//
//  AuthViewController.m
//  NOAHWM
//
//  Created by Ryan on 13-5-17.
//  Copyright (c) 2013年 Ryan. All rights reserved.
//

#import "AuthViewController.h"
#import "NYMemberCenterDataManager.h"
#import "JSON.h"

#define kApplyConfirmDownloaderKey  @"ApplyConfirmDownloaderKey"

@interface AuthViewController ()

@end

@implementation AuthViewController

@synthesize timeLabel,submitButton,timePickView;
@synthesize isMenu;

#pragma mark - IBAction methods
- (IBAction)submitButtonClicked:(id)sender
{
    NSString *subUrl = [NSString stringWithFormat:kApplyConfirmUrl,@"jsonp",[NYMemberCenterDataManager sharedManager].loginMember.accountId,self.timeLabel.text];
    subUrl = [subUrl stringByAppendingString:@"app000020"];
    NSString *url = [NSString stringWithFormat:@"%@%@",kServerAddress,subUrl];
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[RYDownloaderManager sharedManager] requestDataByGetWithURLString:url delegate:self purpose:kApplyConfirmDownloaderKey];
}

#pragma mark - BaseViewController methods
- (void)leftItemTapped
{
    if(self.isMenu)
        [super leftItemTapped];
    else
        [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIViewController methods

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setNaviTitle:@"申请入会"];
    if(!self.isMenu)
    {
        [self setLeftNaviItemWithTitle:nil imageName:@"Icon_Back"];
        [self setRightNaviItemWithTitle:nil imageName:nil];
    }
    
    [self pickerView:self.timePickView didSelectRow:0 inComponent:0];
    [self pickerView:self.timePickView didSelectRow:0 inComponent:1];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.isViewLoaded && !self.view.window)
    {
        [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:kApplyConfirmDownloaderKey];
        self.view = nil;
        self.timeLabel = nil;
        self.timePickView = nil;
        self.submitButton = nil;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:kApplyConfirmDownloaderKey];
    self.timeLabel = nil;
    self.timePickView = nil;
    self.submitButton = nil;
}

- (void)dealloc
{
    [[RYDownloaderManager sharedManager] cancelDownloaderWithDelegate:self purpose:kApplyConfirmDownloaderKey];
    [timeLabel release];
    [timePickView release];
    [submitButton release];
    [super dealloc];
}

#pragma mark - UIPickerViewDataSource methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
    {
        return kAuthDayArray.count;
    }
    else{
        return (kAuthTimeArray).count;
    }
}

#pragma mark - UIPickerViewDelegate methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
    {
        return [kAuthDayArray objectAtIndex:row];
    }
    else
    {
        return [kAuthTimeArray objectAtIndex:row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *leftValue = [kAuthDayArray objectAtIndex:[pickerView selectedRowInComponent:0]];
    NSString *rightValue = [kAuthTimeArray objectAtIndex:[pickerView selectedRowInComponent:1]];
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@ %@",leftValue,rightValue];
}

#pragma mark - RYDownloaderDelegate methods
- (void)downloader:(RYDownloader*)downloader completeWithNSData:(NSData*)data
{
    NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    //去掉被包的jsonp字串
    if(str.length > 7)
        str = [str substringWithRange:NSMakeRange(6, str.length-7)];
    NSDictionary *dict = [str JSONValue];
    if([[dict objectForKey:@"result"] isEqualToString:@"1"])
    {
        [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
        [NYMemberCenterDataManager sharedManager].loginMember.is_auth = @"1";
        self.submitButton.enabled = NO;
        [[NSNotificationCenter defaultCenter] postNotificationName:kRefreshApplyAuthNotificaiton object:nil];
        if(!self.isMenu)
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:kShowYuyueDetailViewNotification object:nil];
        }
    }
    else
        [[RYHUDManager sharedManager] showWithMessage:[dict objectForKey:@"message"] customView:nil hideDelay:2.f];
}
- (void)downloader:(RYDownloader*)downloader didFinishWithError:(NSString*)message
{
    [[RYHUDManager sharedManager] showWithMessage:kNetWorkErrorString customView:nil hideDelay:2.f];
}
@end
